import { useUserStore } from '@/pinia/modules/user'
import { useRouterStore } from '@/pinia/modules/router'
import getPageTitle from '@/utils/page'
import router from '@/router'

const asyncRouterFlag = 0

const whiteList = ['Login', 'Init','404'] // 不需要登录就能进入的页面

router.beforeEach(async(to, from, next) => {
  
  const userStore = useUserStore()
  document.title = getPageTitle(to.meta.title, to)
  if(to.matched.length===0){
    next({ path: '/404'})
  }else if(whiteList.indexOf(to.name) > -1){ // 在白名单中的判断情况(无需登录)
      next()
  }else{
      if(!userStore.userInfo || !userStore.userInfo.userName){ // 未登录
        next({ path: '/login', replace: true })
      }else{ // 已登录
        if(to.meta.keepAlive){
          userStore.addCashPage(to.name) // 添加页面缓存
        }
        next()
      }
  }
  
})
